package com.devshephard.stream;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.groupingBy;

public class StreamDemo    {
    public static void main(String[] args) {

        /**
         * 测试Stream流字符串列表操作
         */
        String str = "KLASDHABASDAVHJA";

        //转列表
        List<String> collect = Stream.of(str.split("")).collect(Collectors.toList());
        System.out.println(collect.toString());
        //去重
        List<String> collect1 = collect.stream().distinct().collect(Collectors.toList());
        System.out.println(collect1.toString());
        //排序
        List<String> collect2 = collect.stream().sorted().collect(Collectors.toList());
        System.out.println(collect2.toString());
        //查找
        String a = collect.stream().filter(v -> v.equals("A")).findAny().orElse(null);
        System.out.println(a.toString());

        /**
         * 测试Stream流对象列表操作
         */
        User user1 = new User();
        user1.setName("张三");
        user1.setSex("男");
        user1.setAge(13);

        User user2 = new User();
        user2.setName("李四");
        user2.setSex("男");
        user2.setAge(12);

        User user3 = new User();
        user3.setName("小芳");
        user3.setSex("女");
        user3.setAge(11);

        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        users.add(user3);

        //实体类列表分组
        Map<String, List<User>> collect3 = users.stream().collect(groupingBy(User::getSex));
        System.out.println(collect3.toString() );
        //实体类列表排序
        List<User> collect4 = users.stream().sorted(Comparator.comparing(User::getAge))
                .collect(Collectors.toList());
        collect4.forEach(item -> {
            System.out.println(item.getAge());
        });

        //是否存在某值
        boolean isHaveZhangSan = users.stream().anyMatch(o -> o.getName().equals("旺五"));
        System.out.println(isHaveZhangSan);

        //并行流
        boolean isHaveZhangSan2 = users.parallelStream()
                .anyMatch(o -> o.getName().equals("张三"));
        System.out.println(isHaveZhangSan2);

    }
}
